package com.thoughtworks.biblioteca.controller;

import com.thoughtworks.biblioteca.dao.MovieDao;
import com.thoughtworks.biblioteca.dao.RateDao;
import com.thoughtworks.biblioteca.model.Movie;
import com.thoughtworks.biblioteca.model.Rate;
import com.thoughtworks.biblioteca.model.User;

import java.util.List;


public class    MovieController {

    private MovieDao movieDao;
    private RateDao rateDao;

    public String getMoviePrintBodyFrom(List<Movie> movies) {
        String stringFormat="%-35s %-20s %-20s\n";
        StringBuffer bf = new StringBuffer("");
        bf.append(String.format(stringFormat,"Movie Name", "Director", "Rating"));



        for (Movie movie : movies) {
            String rateValue=movie.getRate();
            if(rateValue==null||"".equals(rateValue))
                rateValue="N/A";
            bf.append(String.format(stringFormat,movie.getName(),movie.getDirector(),rateValue));
        }

        return bf.toString();
    }

    public void showBooks() {
        System.out.println(getMoviePrintBodyFrom(movieDao.getAllMovies()));
    }

    public MovieDao getMovieDao() {
        return movieDao;
    }

    public String checkoutResultInRateMovie(User user, String movieId) {
        Movie movie=movieDao.findMovieByMovieId(movieId);

        if(movie ==null){
            return "Check the movie ID you typed!!!";
        }

        Rate rate=rateDao.findRateByUserAndMovie(user, movie);

        return getMovieProfileAboutRate(rate);
    }

    private String getMovieProfileAboutRate(Rate rate) {
        StringBuilder builder = new StringBuilder();

        builder.append("Movie Name    My Rating    Average Rating\n");
        builder.append(rate.getMovie().getName());
        builder.append("      ");
        builder.append(getRateStar(Integer.parseInt(rate.getRateValue())));
        builder.append("      ");
        builder.append(getAverageRate(rate.getMovie()));

        return builder.toString();
    }

    public void rateMovie(User user, Movie movie, String rateValue){
        Rate rate=rateDao.findRateByUserAndMovie(user, movie);

        if(rate != null){
            rate.setRateValue(rateValue);
            rateDao.update(rate);
        }else{
            rateDao.add(new Rate(user, movie, rateValue));
        }
    }

    private String getAverageRate(Movie currentMovie) {
        int sumRate=0;
        int rateCount=0;

        for(Rate rate:currentMovie.getRates()){
            rateCount++;
            sumRate+=Integer.parseInt(rate.getRateValue());
        }

        int averageRate=sumRate/rateCount;

        return getRateStar(averageRate);
    }


    private String getRateStar(int averageRate) {
        String rateStar1;

        StringBuffer rateStar=new StringBuffer("");
        int i=0;
        while(i++ < averageRate){
            rateStar.append("*");
        }

        rateStar1=rateStar.toString();
        return rateStar1;
    }

    public Movie getMovie(String movieId) {

        return movieDao.findMovieByMovieId(movieId);
    }

    public void setRateDao(RateDao rateDao) {
        this.rateDao = rateDao;
    }

    public void setMovieDao(MovieDao movieDao) {
        this.movieDao = movieDao;
    }
}
